package com.sc.site.member;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sc.dal.common.pagination.PaginationBean;
import com.sc.dal.mapper.goods.GoodsMapper;
import com.sc.dal.model.goods.Goods;
import com.sc.dal.model.goods.GoodsClass;
import com.sc.service.goods.GoodsClassService;
import com.sc.service.goods.GoodsService;
import com.sc.service.promotion.PromotionService;
import com.sc.util.data.Common;
import com.sc.util.data.DataUtil;
import com.sc.util.exception.ExceptionEnum;

@RequestMapping("/")
@Controller
public class IndexController {
	private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	@Autowired
	private GoodsClassService goodsClassService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GoodsMapper goodsMapper;
	
	
	@Autowired
	private PromotionService  promotionService;

	@RequestMapping("")
	@ResponseBody
	public Object index(HttpServletRequest request,Integer currPage, Integer pageSize) {
		ModelAndView mav = new ModelAndView("/index");
		List<GoodsClass> GoodsClassList = goodsClassService.selectByGoodsClass();
		mav.addObject("GoodsClassList", GoodsClassList);
		List<Map<String, Object>> resultList = null;
		int totalRecords = 0;
		int totalPage = 0;
		pageSize=16;
		//首页人气推荐
		Map<String,Object> mapParam = new HashMap<String, Object>();
		mapParam.put("type", "01");
		List<Goods> goodsHotList = goodsMapper.queryGoodsByClassIdList(mapParam);
		mav.addObject("goodsHotList", goodsHotList);
		List<GoodsClass> classList = (List<GoodsClass>) this.selectList(request);
		mav.addObject("classList", classList);
		List goodsListExt = new ArrayList();
		Map<Object, String> linked = new LinkedHashMap<Object, String>();
		PaginationBean<Map<String, Object>> paginationBean;
		try {
			paginationBean = this.goodsService.getPagingDatas(mapParam, currPage, pageSize, "count", "selectList", GoodsMapper.class);
		    resultList = paginationBean.getPageList();
		    
		    for (int i = 0; i < goodsHotList.size(); i++) {
				Goods goods = (Goods) goodsHotList.get(i);
				Integer id = goods.getId();
				String goodsType = goods.getType();
				//***获取该商品的月销量****************//
				int monthSaleMount =  goodsService.selectGoodsMonthSaleMount(id);	
				goods.setMonthSaleMount(String.valueOf(monthSaleMount));
				Map<String, Object> map = new LinkedHashMap<String, Object>();
				map.put("goodsId", id);
				String proTitle="";
				if(goodsType!=null){
					String goodstypeTemp[] = goodsType.split("-");
					for(int j =0;j<goodstypeTemp.length;j++ ){
						if("09".equals(goodstypeTemp[j])){
							proTitle="包邮、";
						}
			         }
				}
				proTitle += promotionService.getGoodsByPromtion(map);
				goods.setProTitle(proTitle);
				
			}
		    
		    for (int n = 0; n < classList.size(); n++) {
		    	linked = (Map<Object, String>) classList.get(n);
				String classId = String.valueOf(linked.get("id"));
				Map<String, Object> paramMap = new HashMap<String, Object>();
				if(Common.isNotEmpty(classId)){
					paramMap.put("classId", classId);
				}
				List<Goods> goodsList  = goodsService.selectList(paramMap);
				for (int i = 0; i < goodsList.size(); i++) {
					Goods goods = (Goods) goodsList.get(i);
					Integer goodsId = goods.getId();
					String goodsType = goods.getType();
					//***获取该商品的月销量****************//
					int monthSaleMount =  goodsService.selectGoodsMonthSaleMount(goodsId);	
					goods.setMonthSaleMount(String.valueOf(monthSaleMount));
					Map<String, Object> map = new LinkedHashMap<String, Object>();
					map.put("goodsId", goodsId);
					String proTitle="";
					if(goodsType!=null){
						String goodstypeTemp[] = goodsType.split("-");
						for(int j =0;j<goodstypeTemp.length;j++ ){
							if("09".equals(goodstypeTemp[j])){
								proTitle="包邮、";
							}
				         }
					}
					proTitle += promotionService.getGoodsByPromtion(map);
					if(proTitle.endsWith("、")){
						proTitle = proTitle.substring(0, proTitle.length()-1);
					}
					goods.setProTitle(proTitle);
				}
				
				goodsListExt.add(goodsList);
			}
			mav.addObject("goodsListExt", goodsListExt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}

	private Integer Integer(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@RequestMapping("/personal")
	public String personal() {
		return "/personal";
	}
	
	/****
	 * 查询所有的商品分类
	 * @param request
	 * @return
	 */
	public Object selectList(HttpServletRequest request) {
		Map<String, Object> resultMap = new LinkedHashMap<String,Object>();
		//设置查询参数
		Map<String,Object> paramMap = new HashMap<String,Object>();
		String depth = request.getParameter("depth");
		String pId = request.getParameter("parentId");
		Integer parentId = null;
		String code = request.getParameter("code");
		String status = request.getParameter("status");
		if(StringUtils.isNotBlank(depth)){	//父级层级
			paramMap.put("depth", depth);
		}
		if(StringUtils.isNotBlank(pId)){	//父级分类id
			parentId = Integer.valueOf(pId);
			paramMap.put("parentId", pId);
		}
		if(StringUtils.isNotBlank(code)){	//分类编码
			paramMap.put("code", code);
		}
		if(StringUtils.isNotBlank(status)){	//分类状态，设置app首页的一级分类
			paramMap.put("status", status);
		}
		try{
			List<GoodsClass> goodsClassList = this.goodsClassService.selectList(paramMap);
			if(goodsClassList!=null && goodsClassList.size()>0){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
				List<Object> classList = DataUtil.getGoodsCateList(goodsClassList, parentId);
				logger.warn("查找所有商品分类成功！");
				return classList;
//				resultMap.put("data", DataUtil.getGoodsCateList(goodsClassList, parentId));
				
			}else{
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
				logger.warn("查找所有商品分类失败！");
			}
		}catch (Exception e){
			e.printStackTrace();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.warn("查找所有商品分类出错！");
		}
		
		return resultMap;
	} 
	
	/*
	 * 首页改造
	 * @RequestMapping("")
	@ResponseBody
	public Object index(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/index");
		List<GoodsClass> GoodsClassList = goodsClassService
				.selectByGoodsClass();
		mav.addObject("GoodsClassList", GoodsClassList);
		//首页人气推荐
		Map<String,Object> mapParam = new HashMap<String, Object>();
		mapParam.put("type", "01");
		List<Goods> goodsHotList = goodsMapper.queryGoodsByClassIdList(mapParam);
				
		//玩具类
		List<GoodsClass> goodsClassListA = goodsClassService.selectByGoodsClassList(1);
		Map<String,Object> mapA = new HashMap<String, Object>();
		mapA.put("classId", "11");
		List<Goods> goodsListA = goodsMapper.queryGoodsByClassIdList(mapA);
		
		
		//母婴用品类
		List<GoodsClass> goodsClassListB = goodsClassService.selectByGoodsClassList(2);
		Map<String,Object> map = new HashMap<String, Object>();
		if(goodsClassListB!=null && goodsClassListB.size()>0){
			String parentId = goodsClassListB.get(0).getParentId().toString();
			map.put("classId", parentId);
			List<Goods> goodsListB = goodsMapper.queryGoodsByClassIdList(map);
			mav.addObject("goodsListB", goodsListB);
		}
		//奶粉类  //食品类
		List<GoodsClass> goodsClassListC = goodsClassService.selectByGoodsClassList(32);
		Map<String,Object> mapC = new HashMap<String, Object>();
		mapC.put("classId", "3");
		List<Goods> goodsListC = goodsMapper.queryGoodsByClassIdList(mapC);
		mav.addObject("goodsClassListA", goodsClassListA);
		mav.addObject("goodsListA", goodsListA);
		mav.addObject("goodsClassListB", goodsClassListB);
		
		mav.addObject("goodsClassListC", goodsClassListC);
		mav.addObject("goodsListC", goodsListC);
		
		mav.addObject("goodsHotList", goodsHotList);
		
		return mav;
	}*/


}
